package org.easybuy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.easybuy.entity.BuyCar;
import org.easybuy.entity.Product;
import org.easybuy.service.BuyCarService;
import org.easybuy.service.ProductService;
import org.easybuy.serviceimpol.BuyCarServiceImopl;
import org.easybuy.serviceimpol.ProductServiceImpol;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class BuyCarServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String opr = request.getParameter("opr");
		PrintWriter out = response.getWriter();
		if ("insert".equals(opr)) {
			String id = request.getParameter("id");
			int ID = Integer.parseInt(id);
			boolean data = true;
			ProductService productServlet = new ProductServiceImpol();
			BuyCarService buyCarService = new BuyCarServiceImopl();
			BuyCar buyCar = new BuyCar();
			List<Product> list = new ArrayList<Product>();
			list = productServlet.getAllProductsById(ID);
			String name = null;
			String price = null;
			String filName = null;
			for (Product product : list) {
				name = product.getName();
				price = product.getPrice();
				filName = product.getFileName();
			}
			buyCar.setName(name);
			buyCar.setPrice(price);
			buyCar.setFileName(filName);
			buyCar.setNumber("1");
			buyCar.setUserName((String) request.getSession().getAttribute(
					"SHOW_USERNAME"));
			int i = buyCarService.addBuyCar(buyCar);
			if (i > 0) {
				data = true;
			} else {
				data = false;
			}
			out.print(data);
			out.flush();
			out.close();
		} else if ("sele".equals(opr)) {
			String userName = (String) request.getSession().getAttribute(
					"SHOW_USERNAME");
			BuyCarService buyCarServlet = new BuyCarServiceImopl();
			List<BuyCar> list = new ArrayList<BuyCar>();
			list = buyCarServlet.getAllByUserName(userName);
			String newsJSON = JSON.toJSONString(list,
					SerializerFeature.WriteMapNullValue,
					SerializerFeature.WriteNullNumberAsZero,
					SerializerFeature.WriteNullStringAsEmpty);
			out.print(newsJSON);
			out.flush();
			out.close();
		} else if ("ss".equals(opr)) {
			boolean data = true;
			String number = request.getParameter("c");
			String id = request.getParameter("id");
			String price = request.getParameter("price");
			BuyCarService buyCarService = new BuyCarServiceImopl();
			int ID = Integer.parseInt(id);
			BuyCar buyCar = new BuyCar();
			buyCar.setId(ID);
			buyCar.setNumber(number);
			buyCar.setPrice(price);
			int i = buyCarService.upDateNumber(buyCar);
			if (i > 0) {
				data = true;
			} else {
				data = false;
			}
			out.print(data);
			out.flush();
			out.close();
		} else if ("userName".equals(opr)) {
			String userName = (String) request.getSession().getAttribute(
					"SHOW_USERNAME");
			BuyCarService buyCarService = new BuyCarServiceImopl();
			String pirce = buyCarService.zPrice(userName);
			out.print(pirce);
			out.flush();
			out.close();
		} else if ("count".equals(opr)) {
			BuyCarService buyCarServlet = new BuyCarServiceImopl();
			String userName = (String) request.getSession().getAttribute(
					"SHOW_USERNAME");
			int i = buyCarServlet.sumBuyCar(userName);
			out.print(i);
			out.flush();
			out.close();
		} else if ("del".equals(opr)) {
			BuyCarService buyCarServlet = new BuyCarServiceImopl();
			String idString = request.getParameter("id");
			int id = Integer.parseInt(idString);
			BuyCar buyCar = new BuyCar();
			buyCar.setId(id);
			int i = buyCarServlet.dropBuyCar(buyCar);
			out.print(i);
			out.flush();
			out.close();
		}
	}
}
